﻿using System;
using System.Collections.Generic;
using System.IO;
using System.Linq;
using System.Web;
using System.Web.Mvc;

namespace XinYi.CrookedShowBack.Controllers
{
    public class FileUploadController : Controller
    {
        [HttpPost]
        public JsonResult UploadImage(HttpPostedFileBase file)
        {
            //先定义文件
            string fileName = Guid.NewGuid().ToString("N");
            //图片存放的路径
            string filePath = Path.Combine(HttpRuntime.AppDomainAppPath, "Resource/Uploads");
            //如果没有图片
            if (Request.Files.Count == 0)
            {
                return Json(new { Success = false, FilePath = "" });
            }
            //获取图片的扩展名
            string stuf = Path.GetExtension(file.FileName);

            //图片存放路径不存在则创建
            if (!Directory.Exists(filePath))
            {
                Directory.CreateDirectory(filePath);
            }
            //保存图片
            string path = Path.Combine(filePath, fileName + stuf);
            file.SaveAs(path);

            //返回前端
            return Json(new { Success = true, FilePath = "/Resource/Uploads/" + fileName + stuf });

        }
        //public JsonResult UploadImage(HttpPostedFileBase file)
        //{
        //    //先定义文件
        //    //string fileName = Guid.NewGuid().ToString("N");
        //    //图片存放的路径
        //    string filePath = Path.Combine(HttpRuntime.AppDomainAppPath, "Resource/Uploads");
        //    //如果没有图片
        //    if (Request.Files.Count == 0)
        //    {
        //        return Json(new { Success = false, FilePath = "" });
        //    }
        //    //获取图片的扩展名
        //    //string stuf = Path.GetExtension(file.FileName);

        //    //图片存放路径不存在则创建
        //    if (!Directory.Exists(filePath))
        //    {
        //        Directory.CreateDirectory(filePath);
        //    }
        //    //保存图片
        //    string path = Path.Combine(filePath, file.FileName);
        //    file.SaveAs(path);
        //    //返回前端
        //    return Json(new { Success = true, FilePath = "/Resource/Uploads/" + file.FileName });
        //}
        //#endregion      
    }
}